package com.photoeditor.demo.util;

import android.os.Build;
import android.text.TextUtils;

import com.android.gpuimage.util.Base64;
import com.android.gpuimage.util.CryptTool;
import com.android.util.NativeVideoUtil;
import com.common.base.ComponentContext;

import java.security.PublicKey;
import java.util.UUID;


/**
 * 生成请求服务器资源的特征码
 * Created by edward on 17-11-6.
 */

public class FeatureCodeUtil {

    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuVBaBkvotRbLOT5qMl1O2OgfR\n" +
            "IIq3QQZ2SaIHqSivsGVPlkIv/wsLqB2tIqYpFh5x4X0xdeYrFqOfMlXhMLp4jP4g\n" +
            "Spjhz2E7ZLH6Ofb3NR9FdFqwW104MPllv6SVogku5AN1mQxYaExNzDbZMiunzWp6\n" +
            "+FvgS0A3uNxoP75eCQIDAQAB";

    private static String sEncryptUniqueId;
    private static String sUniqueId;

    /**
     * 获取加密后的设备唯一特征码
     *
     * @return
     */
    private static String getEncryptUniqueId() {
        if (sEncryptUniqueId != null) {
            return sEncryptUniqueId;
        }
        String destString = CryptTool.encrypt(getUniqueId(), NativeVideoUtil.getOxString());
        try {
            if (destString != null) {
                PublicKey publicKey = RSAUtils.loadPublicKey(PUBLIC_KEY);
                destString = Base64.encodeBytes(RSAUtils.encryptData(destString.getBytes(), publicKey), false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        sEncryptUniqueId = Base64.replaceIllegalUrlString(destString);
        return sEncryptUniqueId;
    }

    /**
     * 获取设备唯一特征码
     *
     * @return
     */
    private static String getUniqueId() {
        if (sUniqueId != null) {
            return sUniqueId;
        }
        String serial = Build.SERIAL;
        String customDeviceId = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 位
        sUniqueId =  new UUID(customDeviceId.hashCode(), serial.hashCode()).toString();
        return sUniqueId;
    }

    /**
     * 使用机型和系统版本生成基础加密信息
     * @return
     */
    private static String getNormalEncryptInfo() {
        return Base64.replaceIllegalUrlString(CryptTool.encrypt(Build.MODEL + "/" + Build.ID, NativeVideoUtil.getOxString()));
    }

    public static String addFeatureString(String url) {
        if (TextUtils.isEmpty(url) || url.contains(generateQueryString())) {
            return url;
        }
        int queryIndex = url.indexOf("?");
        if (queryIndex == -1) {
            return url + "?" + generateQueryString();
        }
        String baseUrl = url.substring(0, queryIndex);
        String queryString = url.substring(queryIndex + 1, url.length());
        if (TextUtils.isEmpty(queryString)) {
            return baseUrl + "?" + generateQueryString();
        } else {
            return url + "&" + generateQueryString();
        }
    }

    private static String sQueryString;
    private synchronized static String generateQueryString() {
        if (sQueryString == null) {
            sQueryString = getNormalEncryptInfo() + "-" + getUniqueId() + "="
                    + getEncryptUniqueId() + "-"
                    + ComponentContext.getContext().getPackageName() + "-"
                    + AndroidDevice.getVersionCode(ComponentContext.getContext());
        }
        return sQueryString;
    }
}
